/**
#include "Qnet.h"
class NetworkAccess : public QObject {
    Q_OBJECT
public:
    NetworkAccess() {
        manager = new QNetworkAccessManager(this);
        connect(manager, &QNetworkAccessManager::finished, this, &NetworkAccess::onReplyFinished);
    }
    void fetchData() {
        QUrl url("https://api.seniverse.com/v3/weather/now.json?key=SozYmp2ySytux_l4Y&location=beijing&language=zh-Hans&unit=c"); // 替换为你的API URL
        QNetworkRequest request(url);
        manager->get(request);
    }
private slots:
    void onReplyFinished(QNetworkReply *reply) {
        if (reply->error() == QNetworkReply::NoError) {
            QString response = reply->readAll(); // 获取响应数据
            QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
            if (!doc.isNull()) {
                // 处理JSON数据...
            } else {
                qDebug() << "Invalid JSON";
            }
        } else {
            qDebug() << "Error:" << reply->errorString(); // 处理错误情况
        }
        reply->deleteLater(); // 删除reply对象，防止内存泄漏
    }
private:
    QNetworkAccessManager *manager;
};
**/
// Client.cpp
//#include "Qnet.h"

/**
Client::Client(QObject *parent) : QObject(parent)
{
    m_socket = new QTcpSocket(this);
    connect(m_socket, &QTcpSocket::connected, this, &Client::onConnected);
    connect(m_socket, &QTcpSocket::readyRead, this, &Client::onReadyRead);
    connect(m_socket, &QTcpSocket::disconnected, this, &Client::onDisconnected);
}

void Client::connectToServer(const QString &host, quint16 port)
{
    m_socket->connectToHost(host, port);
}

void Client::sendMessage(const QString &message)
{
    if(m_socket->state() == QTcpSocket::ConnectedState) {
        QByteArray data;
        quint32 len = message.size();
        data.append((char*)&len, 4);
        data.append(message.toUtf8());
        m_socket->write(data);
    }
}

void Client::onConnected()
{
    qDebug() << "Connected to server!";
}

void Client::onReadyRead()
{
    m_buffer.append(m_socket->readAll());

    while (m_buffer.size() >= 4) {
        quint32 packetLength;
        memcpy(&packetLength, m_buffer.constData(), 4);

        if (m_buffer.size() - 4 >= packetLength) {
            QByteArray packet = m_buffer.mid(4, packetLength);
            m_buffer.remove(0, 4 + packetLength);

            qDebug() << "Received response:" << QString::fromUtf8(packet);
        } else {
            break;
        }
    }
}

void Client::onDisconnected()
{
    qDebug() << "Disconnected from server";
}

ApiClient ApiClient(){
    manager = new QNetworkAccessManager(this);
}
ApiClient::fetchData(){
    QUrl url("https://api.seniverse.com/v3/weather/now.json?key=SozYmp2ySytux_l4Y&location=beijing&language=zh-Hans&unit=c"); // 替换为你的API URL
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); // 根据需要设置请求头
    QNetworkReply *reply = manager->get(request);
    connect(reply, &QNetworkReply::finished, this, &ApiClient::onReplyFinished); // 连接信号到槽函数处理响应数据
}
ApiClient onReplyFinished(){
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
    if (reply->error() == QNetworkReply::NoError) {
        QByteArray responseData = reply->readAll(); // 获取响应数据
        QString responseString = QString::fromUtf8(responseData); // 将响应转换为字符串（如果需要）
        // 处理responseString或responseData，例如解析JSON等...
        // 使用 QJsonDocument 和 QJsonArray/QJsonObject 解析JSON数据示例：
        QJsonDocument doc = QJsonDocument::fromJson(responseData); // 将JSON数据转换为QJsonDocument对象进行进一步处理...
        if (!doc.isNull() && doc.isObject()) { qDebug() << "Error:" << reply->abort();}}

}
**/

#include "Qnet.h"

class NetworkManagers:public QObject
{
public:
    NetworkManagers() {
        manager = new QNetworkAccessManager(this);
    }
    void get(const QUrl &url) {
        QNetworkRequest request(url);
        QNetworkReply *reply = manager->get(request);
        connect(reply, &QNetworkReply::finished, this, &NetworkManagers::onFinished);
    }

private slots:
    void onFinished() {
        QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
        if (reply->error() == QNetworkReply::NoError) {
            qDebug() << "Success:" << reply->readAll();
        } else {
            qDebug() << "Error:" << reply->errorString();
        }
        reply->deleteLater(); // 不再需要时删除reply对象
    }

private:
    QNetworkAccessManager *manager;
};



